
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">算法导入</h4>
</div>
<form id="defaultForm" method="post" class="form-horizontal" action="/project/arithmeticManage/manage_save">
    <div class="modal-body">

            <div class="form-group">
                <label class="col-lg-3 control-label">名称</label>
                <div class="col-lg-6">
                    <input type="text" class="form-control" name="name" id="name"/>
                </div>
            </div>

        <div class="form-group">
                <label class="col-lg-3 control-label">版本</label>
                <div class="col-lg-6">
                    <input type="text" class="form-control" name="version" id="version"/>
                </div>
            </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">备注</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="remark" id="remark"/>
            </div>
        </div>

        <div class="form-group">
                <label class="col-lg-3 control-label">文件</label>
            <div class="col-lg-6">
                <input type="text" id="fileName" class="form-control" name="fileName" />
            </div>
            <div  class="col-lg-3">
                <button class="btn btn-success fileinput-button" type="button">打开</button>
                <input type="file" id="jobData" onchange="loadFile(this.files[0])" style="position:absolute;top:0;left:0;font-size:34px; opacity:0" >
            </div>
        </div>


    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="submit" class="btn btn-primary">保存</button>
    </div>
</form>

<script>
    function loadFile(file){
        $("#fileName").val(file.name);
    }
    initCheck();

    function initCheck() {
        $('#defaultForm')
            .bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    name: {
                        message: '不是有效的名称',
                        validators: {
                            notEmpty: {
                                message: '名称不能为空'
                            },
                            stringLength: {
                                min: 1,
                                max: 30,
                                message: '用户名不能超过30个字符'
                            }
                        }
                    },
                    version:{
                        message: '不是有效的版本',
                        validators: {
                            notEmpty: {
                                message: '版本不能为空'
                            },
                            stringLength: {
                                min: 1,
                                max: 30,
                                message: '用户名不能超过30个字符'
                            }
                        }
                    }
                }
            })
            .on('success.form.bv', function(e) {
                // 阻止默认提交事件
                e.preventDefault();
                // 获取form表单
                var $form = $(e.target);
                // Get the BootstrapValidator instance

                var formData = new FormData();
                var file=$('#jobData')[0].files[0];
                var fileName=$("#fileName").val();
                if(file==null || fileName==""){
                    alert("请选择上传的文件");
                    return;
                }

                formData.append("fileName",fileName);
                formData.append("file",file);
                formData.append("name", $('#name').val());
                formData.append("remark", $('#remark').val());
                formData.append("version", $('#version').val());

                $.ajax({
                    url : $form.attr('action'),
                    type : "post",
                    data : formData,
                    cache : false,//上传文件无需缓存
                    processData : false,//用于对data参数进行序列化处理 这里必须false
                    contentType : false, //必须
                    dataType:'json',
                    success:function (result) {
                        if(result.type == "alert"){
                            alert(result.message);
                        }
                        $("#modal").modal("hide");
                        //刷新跳转至第一页
//                        $("#dataTable").bootstrapTable("selectPage",1);
                        $("#dataTable").bootstrapTable('refresh', {url: '/project/arithmeticManage/manage_data'});
                    },
                    error:function () {
                        console.log("error")
                    }
                });

            });
    }

</script>

